package com.fangmou.ing.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * Created by IntelliJ IDEA.
 * User: liming
 * Date: 2011-3-1
 * Time: 13:10:34
 * To change this template use File | Settings | File Templates.
 */
public class DateUtils {
    public static String FORMAT_OF_FULL = "yyyyMMddHHmmssSSS";
    public static long TIME_OF_MINUTE = 1000 * 60;
    public static long TIME_OF_HOUR = TIME_OF_MINUTE * 60;
    public static long TIME_OF_DAY = TIME_OF_HOUR * 24;

    public static String getFormatStringOfNow(String format) {
        Calendar cal = GregorianCalendar.getInstance();
        return getFormatString(cal.getTime().getTime(), format);
    }

    public static String getFormatString(long time, String format) {
        SimpleDateFormat bartDateFormat = new SimpleDateFormat(format);
        return bartDateFormat.format(new Long(time));
    }

    /**
     * 将时间格式化为文字描述
     *
     * @param mss      要转换的毫秒数
     * @param language 格式文字的语言 cn:中文;en:英文(默认)
     * @return 格式串 比如*天*小时*分钟*秒
     */
    public static String formatPeriod(long mss, String language) {
        long days = mss / TIME_OF_DAY;
        long hours = (mss % TIME_OF_DAY) / TIME_OF_HOUR;
        long minutes = (mss % TIME_OF_HOUR) / TIME_OF_MINUTE;
        long seconds = (mss % TIME_OF_MINUTE) / 1000;
        if ("cn".equals(language))
            return (days != 0 ? days + "天" : "")
                    + (hours != 0 ? hours + "小时" : "")
                    + (minutes != 0 ? minutes + "分钟" : "")
                    + (seconds != 0 ? seconds + "秒" : "");
        else
            return (days != 0 ? days + " days " : "")
                    + (hours != 0 ? hours + " hours " : "")
                    + (minutes != 0 ? minutes + " minutes " : "")
                    + (seconds != 0 ? seconds + " seconds " : "");
    }
}
